using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections.Generic;

namespace WindowFinder
{
	/// <summary>
	/// Summary description for Chooser.
	/// </summary>
	public class Chooser : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListBox VisListBox;
		private System.Windows.Forms.Button OkButton;
		private System.Windows.Forms.CheckBox InvisibleBox;
		private System.Windows.Forms.CheckBox NoTitleBox;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Chooser()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.InvisibleBox = new System.Windows.Forms.CheckBox();
			this.OkButton = new System.Windows.Forms.Button();
			this.VisListBox = new System.Windows.Forms.ListBox();
			this.NoTitleBox = new System.Windows.Forms.CheckBox();
			this.SuspendLayout();
			// 
			// InvisibleBox
			// 
			this.InvisibleBox.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.InvisibleBox.Location = new System.Drawing.Point(8, 185);
			this.InvisibleBox.Name = "InvisibleBox";
			this.InvisibleBox.Size = new System.Drawing.Size(152, 24);
			this.InvisibleBox.TabIndex = 2;
			this.InvisibleBox.Text = "Show invisible windows";
			this.InvisibleBox.CheckedChanged += new System.EventHandler(this.InvisibleBox_CheckedChanged);
			// 
			// OkButton
			// 
			this.OkButton.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.OkButton.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.OkButton.Location = new System.Drawing.Point(206, 203);
			this.OkButton.Name = "OkButton";
			this.OkButton.TabIndex = 1;
			this.OkButton.Text = "Accept";
			// 
			// VisListBox
			// 
			this.VisListBox.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.VisListBox.Location = new System.Drawing.Point(8, 8);
			this.VisListBox.Name = "VisListBox";
			this.VisListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
			this.VisListBox.Size = new System.Drawing.Size(274, 173);
			this.VisListBox.TabIndex = 0;
			// 
			// NoTitleBox
			// 
			this.NoTitleBox.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.NoTitleBox.Location = new System.Drawing.Point(8, 209);
			this.NoTitleBox.Name = "NoTitleBox";
			this.NoTitleBox.Size = new System.Drawing.Size(160, 24);
			this.NoTitleBox.TabIndex = 3;
			this.NoTitleBox.Text = "Show windows with no title";
			this.NoTitleBox.CheckedChanged += new System.EventHandler(this.NoTitleBox_CheckedChanged);
			// 
			// Chooser
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(290, 234);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.NoTitleBox,
																		  this.InvisibleBox,
																		  this.OkButton,
																		  this.VisListBox});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.MinimumSize = new System.Drawing.Size(296, 250);
			this.Name = "Chooser";
			this.Text = "Window Picker";
			this.Load += new System.EventHandler(this.Chooser_Load);
			this.ResumeLayout(false);

		}
		#endregion
		//public property for retrieving selected windows
		public System.Windows.Forms.ListBox.SelectedObjectCollection SelectedWindows
		{
			get
			{
				return(VisListBox.SelectedItems);
			}
		}
		public Window SelectedWindow
		{
			get
			{
				return (Window)VisListBox.SelectedItem;
			}
		}
		//load all available windows
		private void Chooser_Load(object sender, System.EventArgs e)
		{
			VisListBox.Items.Clear();

			VisListBox.BeginUpdate();

			//enumerate thru Windows collection
			List<Window> wndList = new WindowEnumerator(InvisibleBox.Checked,NoTitleBox.Checked).FindAll();
			foreach (Window wnd in wndList) {VisListBox.Items.Add(wnd);}
            
            for (int i = 0; i < VisListBox.Items.Count ; i++)
            {
                VisListBox.SetSelected(i, true);
            }

			VisListBox.EndUpdate();
            
		}
		private void InvisibleBox_CheckedChanged(object sender, System.EventArgs e)
		{
			this.Chooser_Load(this,EventArgs.Empty);
		}
		private void NoTitleBox_CheckedChanged(object sender, System.EventArgs e)
		{
            this.Chooser_Load(this,EventArgs.Empty);
		}
		public bool MultiSelect
		{
			set
			{
				if (!value)
					VisListBox.SelectionMode = SelectionMode.One;
			}
		}
	}
}
